﻿using System;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerInputMgr : IInputMgr {

	readonly PlayerInput _playerInput;
	readonly Dictionary<string, Action> _callBacks;
	readonly Keyboard _keyboard;
	readonly Key _jumpKey;
	readonly Key _headWeaponKey;
	readonly Key _tickKey;
	readonly Key _bowKey;
	readonly Key _reboundKey;
	readonly Key _slideKey;
	readonly Key _davidKey;
	public float XInput { get; set; }
	public bool Jump { get => _keyboard[_jumpKey].wasPressedThisFrame; set { } }
	public bool DownInput { get; set; }
	public bool WalkInput { get; set; }
	public bool SlideInput { get => _keyboard[_slideKey].wasPressedThisFrame; set { } }
	public bool UseItemInput { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
	public bool Up { get; set; }
	public bool HandWeaponInput { get => _keyboard[_headWeaponKey].wasPressedThisFrame; set { } }
	public bool TickInput { get => _keyboard[_tickKey].wasPressedThisFrame; set { } }
	public bool BowInput { get => _keyboard[_bowKey].wasPressedThisFrame; set { } }
	public bool ReboundInput { get => _keyboard[_reboundKey].wasPressedThisFrame; set => throw new NotImplementedException(); }
	public bool DavidInput { get => _keyboard[_davidKey].isPressed; set => throw new NotImplementedException(); }

	public PlayerInputMgr() {
		_playerInput = new PlayerInput();
		_playerInput.Enable();
		_callBacks = new Dictionary<string, Action>();
		_keyboard = InputSystem.GetDevice<Keyboard>();
		_jumpKey = GetKeyFromBinding(_playerInput.PlayerCtrl.Jump.bindings[0]);
		_keyboard = Keyboard.current;
        _jumpKey = GetKeyFromBinding(_playerInput.PlayerCtrl.Jump.bindings[0]);
		//_keyboard = InputSystem.GetDevice<Keyboard>();
		_keyboard = Keyboard.current;
		_jumpKey = GetKeyFromBinding(_playerInput.PlayerCtrl.Jump.bindings[0]);
		_headWeaponKey = GetKeyFromBinding(_playerInput.PlayerCtrl.HandAtk.bindings[0]);
		_tickKey = GetKeyFromBinding(_playerInput.PlayerCtrl.TickAtk.bindings[0]);
		_bowKey = GetKeyFromBinding(_playerInput.PlayerCtrl.BowAtk.bindings[0]);
		_reboundKey = GetKeyFromBinding(_playerInput.PlayerCtrl.Rebound.bindings[0]);
		_slideKey = GetKeyFromBinding(_playerInput.PlayerCtrl.Slide.bindings[0]);
		_davidKey = GetKeyFromBinding(_playerInput.PlayerCtrl.David.bindings[0]);

	}

	public void AddCallBack(string name, Action callBack)
		=> _callBacks.Add(name, callBack);

	public void BindCallBack() {
		//XDir
		_playerInput.PlayerCtrl.XDir.performed += ctx => {
			XInput = ctx.ReadValue<float>();
			_callBacks["XInput"]();
			//_callBacks["ChangeFaceDir"]();
		};
		_playerInput.PlayerCtrl.XDir.canceled += ctx => {
			XInput = 0;
		};


		//Up
		_playerInput.PlayerCtrl.UpDIr.started += ctx => Up = true;
		_playerInput.PlayerCtrl.UpDIr.canceled += ctx => Up = false;


		//Walk
		_playerInput.PlayerCtrl.WalkHold.started += ctx => WalkInput = true;
		_playerInput.PlayerCtrl.WalkHold.canceled += ctx => WalkInput = false;


		//Slide
		//_playerInput.PlayerCtrl.Slide.started += ctx => SlideInput = true;
		//_playerInput.PlayerCtrl.Slide.canceled += ctx => SlideInput = false;


		//Crouch
		_playerInput.PlayerCtrl.YDownDir.performed += ctx => DownInput = true;
		_playerInput.PlayerCtrl.YDownDir.canceled += ctx => DownInput = false;

		//SwitchWeapon
		_playerInput.PlayerCtrl.SwitchWeapon.started += ctx => _callBacks["SwitchWeapon"]();

		_playerInput.PlayerCtrl.David.started += ctx => Time.timeScale = 0.1f;
		_playerInput.PlayerCtrl.David.canceled += ctx => Time.timeScale = 1;
	}

	Key GetKeyFromBinding(InputBinding binding) {
		var keyString = binding.effectivePath["<Keyboard>/".Length..];
		keyString = char.ToUpper(keyString[0]) + keyString[1..];
		return (Key)System.Enum.Parse(typeof(Key), keyString);
	}
}